package com.bishe.psy.service.impl;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.dao.ArticleReplyMapper;
import com.bishe.psy.exception.ApplicationException;
import com.bishe.psy.model.ArticleReply;
import com.bishe.psy.service.IArticleReplyService;
import com.bishe.psy.service.IArticleService;
import com.bishe.psy.utils.StringUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-04-21
 * Time: 9:29
 */

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;

    @Resource
    private IArticleService iArticleService;

    @Override
    public void create(ArticleReply articleReply) {

        //1.参数校验
        if(articleReply == null || articleReply.getArticleId() == null
        || articleReply.getPostUserId() == null || StringUtil.isEmpty(articleReply.getContent())
        || articleReply.getCurrentIdentity() == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.更新文章回复表，创建文章回复对像
        articleReply.setDeleteState((byte) 0);
        Date date = new Date();
        articleReply.setCreateState(date);
        articleReply.setUpdateState(date);

        //3.插入到数据库
        int row = articleReplyMapper.insertSelective(articleReply);

        //4.判断插入操作是否成功
        if(row != 1) {
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        //5.更新文章表该篇文章的回复数
        iArticleService.addOneReplyCountById(articleReply.getArticleId());

        log.info("更新成功");

    }

    @Override
    public List<ArticleReply> selectArticleReplyById(Long articleId) {

        //1.参数校验检查
        if(articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.调用数据库
        List<ArticleReply>  articleList = new ArrayList<>();
        List<ArticleReply> articleStuReplies = articleReplyMapper.selectStuAll(articleId);
        List<ArticleReply> articleConReplies = articleReplyMapper.selectConAll(articleId);
        List<ArticleReply> articleAdmReplies = articleReplyMapper.selectAdmAll(articleId);

        //3.拼接该条文章的所有回复记录
        articleList.addAll(articleStuReplies);
        articleList.addAll(articleConReplies);
        articleList.addAll(articleAdmReplies);

        //4.对拼接数据升序
        Collections.sort(articleList, new Comparator<ArticleReply>() {
            @Override
            public int compare(ArticleReply o1, ArticleReply o2) {
                return o1.getCreateState().compareTo(o2.getCreateState());
            }
        });

        //5.对升序后的数据降序
        Collections.reverse(articleList);

        return articleList;

    }
}
